package java_class_work.week07.jdbc;

import java_class_work.week07.JDBCUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;

/**
 * @Auther: admin
 * @Date: 2022/10/25 10:59
 * @Description:
 */

public class Delete {
    public static void main(String[] args) {
        String username = input();
        Connection con = JDBCUtils.getConnection();

        String sql = "delete from user where username = ?";
        PreparedStatement ps = null;
        try {
            // 关闭自动提交事物
            con.setAutoCommit(false);

            ps = con.prepareStatement(sql);
            ps.setString(1, username);
            int i = ps.executeUpdate();

            // 提交事物
            con.commit();
            System.out.println("effect rows" + i);
        } catch (SQLException e) {
            try {
                // 出现异常 -》 回滚事务
                if (con != null) con.rollback();
            } catch (SQLException e1) {
                e1.printStackTrace();
            }
            e.printStackTrace();
        } finally {
            try {
                ps.close();
                con.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            JDBCUtils.close();
        }
    }


    private static String input() {
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入想注销的账号：");
        return scanner.nextLine();
    }
}
